﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainPlayer : MonoBehaviour {

	public float _speed = 2.0f;

	private Animator m_anmi = null;

	public PolygonCollider2D weapon;

	private float m_timer;

	private bool isAttack;

	// Use this for initialization
	void Start () {
		m_anmi = GetComponent<Animator> ();
	}

	void Walk() {
		transform.Translate (new Vector2 (Input.GetAxisRaw("Horizontal"), 0) * Time.deltaTime * _speed, Space.World);
		m_anmi.SetInteger ("WalkDir", (int)Input.GetAxisRaw("Horizontal"));

		if (Input.GetAxis ("Horizontal") > 0) {
			transform.rotation = Quaternion.Euler (0, 180, 0);
		}

		if (Input.GetAxis ("Horizontal") < 0) {
			transform.rotation = Quaternion.Euler (0, 0, 0);
		}
	}

	void Attack() {
		m_anmi.SetBool ("Attack", false);
		if (Input.GetKey (KeyCode.J)) {
			m_anmi.SetBool ("Attack", true);
			isAttack = true;
		}
	}
	
	// Update is called once per frame
	void Update () {
		Walk ();
		Attack ();

		if (isAttack) {
			m_timer += Time.deltaTime;

			if (m_timer > 0.5f && m_timer < 0.8f) {
				weapon.enabled = true;
			}

			if (m_timer >= 0.8f) {
				m_timer = 0;
				weapon.enabled = false;
				isAttack = false;
			}
		}
	}
}
